package mvc;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class MainView {
	protected Shell shell;
	protected Menu menu;
	protected MenuItem userItem;
	protected Menu userItemMenu;
	protected MenuItem newUserItem;
	protected MenuItem loginUserItem;
	protected MenuItem deleteUserItem;
	protected Menu orderItemMenu;
	protected MenuItem orderItem;
	protected MenuItem newOrderItem;
	protected MenuItem editOrderItem;
	protected MenuItem deleteOrderItem;
	protected MenuItem menuItem;
	protected Menu menuItemMenu;
	protected MenuItem newMenuItem;
	protected MenuItem editMenuItem;
	protected MenuItem deleteMenuItem;
	protected Listener controller;
	protected IComposites composite = null;
	

	protected MainView()
	{
		controller = new MainController(this);
	}
	
	public static void main(String[] args) {
		try {
			MainView window = new MainView();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	
	protected void createContents() {
		shell = new Shell();
		shell.setSize(700, 500);
		shell.setText("Lunchy");
		shell.setLayout(null);
		menu = createMenu();
	}

	Menu createMenu() {
		//create menu
		Menu menu = new Menu(shell, SWT.BAR);
		//create menu - User
		userItem = new MenuItem(menu, SWT.CASCADE);
		userItem.setText("User");		
		//User - menu too
		userItemMenu = new Menu(menu);
		userItem.setMenu(userItemMenu);
		userItem.setEnabled(false);
		// create menu - User - New
		newUserItem = new MenuItem(userItemMenu, SWT.NONE);
		newUserItem.setText("New");		 
		newUserItem.addListener(SWT.Selection, controller);		
		// create menu - User - Login
		loginUserItem = new MenuItem(userItemMenu, SWT.NONE);
		loginUserItem.setText("Login");
		loginUserItem.addListener(SWT.Selection, controller);
		// create menu - User - Delete
		deleteUserItem = new MenuItem(userItemMenu, SWT.NONE);
		deleteUserItem.setText("Delete");
		deleteUserItem.setEnabled(false);
		//create menu - Order		
		orderItem = new MenuItem(menu, SWT.CASCADE);
		orderItem.setText("Order");		
		//Order - menu too
		orderItemMenu = new Menu(menu);
		orderItem.setMenu(orderItemMenu);
		// create menu - Order - New
		newOrderItem = new MenuItem(orderItemMenu, SWT.NONE);
		newOrderItem.setText("New");
		newOrderItem.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent arg0) {				
				if ((composite!=null) && !(composite instanceof NewOrderComposite)){
					composite.dispose();					
					composite = new NewOrderComposite(shell, SWT.NONE);
				}
				else {
					composite = new NewOrderComposite(shell, SWT.None);					
				}
				shell.layout(true, true);
			}
			
		});
		// create menu - Order - Edit
		editOrderItem = new MenuItem(orderItemMenu, SWT.NONE);
		editOrderItem.setText("Edit");
		// create menu - Order - Delete
		deleteOrderItem = new MenuItem(orderItemMenu, SWT.NONE);
		deleteOrderItem.setText("Delete");
		//create menu - Menu
		menuItem = new MenuItem(menu, SWT.CASCADE);
		menuItem.setText("Menu");		
		//Order - menu too
		menuItemMenu = new Menu(menu);
		menuItem.setMenu(menuItemMenu);
		// create menu - Menu - New
		newMenuItem = new MenuItem(menuItemMenu, SWT.NONE);
		newMenuItem.setText("New");
		newMenuItem.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if ((composite!=null) && !(composite instanceof NewMenuComposite)){
					composite.dispose();					
					composite = new NewMenuComposite(shell, SWT.NONE);
				}
				else {
					composite = new NewMenuComposite(shell, SWT.None);					
				}
				shell.layout(true, true);
			}
			
		});
		// create menu - Menu - Edit
		editMenuItem = new MenuItem(menuItemMenu, SWT.NONE);
		editMenuItem.setText("Edit");
		// create menu - Menu - Delete
		deleteMenuItem = new MenuItem(menuItemMenu, SWT.NONE);
		deleteMenuItem.setText("Delete");		
		shell.setMenuBar(menu);
		shell.setLayout(new FormLayout());
//		new NewOrderComposite(shell, SWT.NONE);
		//new NewMenuComposite(shell, SWT.NONE);		
		return menu;
	}	
}
